<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
  /*
    JQuery.Callbacks是jQuery1.7+之后引入的，用来进行函数队列的add、remove、fire、lock等操作
Callbacks对象其实就是一个函数队列，获得Callbacks对象之后 ，就可以向这个集合中增加或者删除函数。add和remove功能相反，函数参数是相同的，empty()删除回调列表中的所有函数
  */
  let callbacks = $.Callbacks();

  let a1 = () => alert(1);
  let a2 = () => alert(2);
  let a3 = () => alert(13);
  callbacks.add(a1);
  callbacks.add(a2);
  callbacks.add(a3);
  callbacks.remove(a3);
  callbacks.fire();

  /*
function Callbacks() {
  let observers = [];
  function add(observer) {
      observers.push(observer);
  }
  function remove(observer) {
      let index = observers.indexOf(observer);
      if (index != -1)
          observers.splice(index, 1);
  }
  function fire() {
      observers.forEach(observer => observer());
  }
  return {
      add,
      remove,
      fire
  }
}
  */
</script>
</body>
</html>
